﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;

namespace FileMd5Checker
{
    public class CaculateMd5
    {
        public static string GetMD5FromString(string str)
        {
            byte[] data = Encoding.GetEncoding("utf-8").GetBytes(str);
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] bytes = md5.ComputeHash(data);
            //return bytes.Aggregate(str, (current, t) => current + t.ToString("x2"));
	        string strmd5 = "";
	        for (int i = 0; i < bytes.Length; i++)
	        {
                strmd5 += bytes[i].ToString("x2");
	        }
            return strmd5;
        }
        public static string GetMD5FromFile(string path)
        {
            FileStream file = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            byte[] hashByte = md5.ComputeHash(file);
            string str = System.BitConverter.ToString(hashByte);
            str = str.Replace("-", "");
            return str;
        }
    }
}
